package com.zyd.blog.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zyd.blog.business.annotation.BussinessLog;
import com.zyd.blog.business.enums.PlatformEnum;
import com.zyd.blog.homeart.service.IFineArtService;
import com.zyd.blog.homeart.util.ResponseObject;
import com.zyd.blog.persistence.beans.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
* <p>
    *  前端控制器
    * </p>
*
* @author liusongqi
* @since 2020-03-05
*/
@RestController
@RequestMapping("/fineArt")
public class FineArtController {
    private final Logger logger = LoggerFactory.getLogger(FineArtController.class);
@Autowired
private IFineArtService fineArtService;

    //软装产品 全部
    @RequestMapping(value = "/fineArtAll", method = RequestMethod.POST)
    @BussinessLog(value = "/fineArt/fineArtAll 艺术精品 全部 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject fineArtAll(String userId,int page,int count) {
        ResponseObject ob = new ResponseObject();

        ob.setCode(0);
        ob.setData(fineArtService.soft(userId,page,count));
        ob.setMsg("success");
        return ob;
    }
    //添加文章
    @RequestMapping(value = "/insertFineArt", method = RequestMethod.POST)
    public ResponseObject insertFineArt(String fineTitle, String fineContent, String draft, String fineImage, String list,String userId) {
        logger.info("/fineArt/insertFineArt 发布艺术精品，fineTitle={},fineContent={},draft={},fineImage={},list={},userId={}",fineTitle,fineContent,draft,fineImage,list,userId);
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer = fineArtService.insertFineArt(fineTitle,fineContent,draft,fineImage,list,userId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }





    /**
     *     根据文章 id  点赞   点赞数量加1
     * state 0收藏 1点赞 2评论 3分享
     */
    @RequestMapping(value = "/fabulous", method = RequestMethod.POST)
    public ResponseObject fabulous(String fineId,String userId,String category,String state) {
        logger.info("/fineArt/fabulous 进行点赞 fineId={},userId={},category={},state={} ",fineId,userId,category,state);
        ResponseObject ob = new ResponseObject();
        Integer sort1;
        try {
            sort1 = fineArtService.fabulous(fineId,userId,category,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(sort1);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     根据文章 id  取消点赞   点赞数量减1
     * state 0收藏 1点赞 2评论 3分享
     */
    @RequestMapping(value = "/reduceZan", method = RequestMethod.POST)
    public ResponseObject reduceZan(String fineId,String userId,String category,String state) {
        logger.info("/fineArt/reduceZan 取消点赞  fineId={},userId={},category={},state={} ",fineId,userId,category,state);
        ResponseObject ob = new ResponseObject();
        Integer integer;
        try {
            integer = fineArtService.reduceZan(fineId,userId,category,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }







    /**
     *     推荐文章r  style  风格  place 场所
     *     先计算热度，若现在看的场所是别墅，就取别墅热度前10的数据。
     */
    @RequestMapping(value = "/recommendArticle", method = RequestMethod.POST)
    public ResponseObject recommendArticle(String style,String place) {
        logger.info("/softProduct/recommendArticle 推荐文章  style={},place={} ",style,place);
        ResponseObject ob = new ResponseObject();
        List<FineArt> softProducts;
        try {
            softProducts = fineArtService.recommendArticle(style, place);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(softProducts);
        ob.setMsg("success");
        return ob;
    }



    /**
     *     根据文章 id  获取具体内容
     */
    @RequestMapping(value = "/specific", method = RequestMethod.POST)
    public ResponseObject specific(String fineId,String userId,String state)  {
        logger.info("/fineArt/specific 获取具体内容, fineId={},userId={},state={}", fineId, userId, state);
        ResponseObject ob = new ResponseObject();
        Map<String,Object> specific;
        try {
            specific = fineArtService.specific(fineId, userId,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(specific);
        ob.setMsg("success");
        return ob;
    }

    /**
     *     我添加的 艺术精品
     */
    @RequestMapping(value = "/myAddFine", method = RequestMethod.POST)
    public ResponseObject myAddFine(String userId,int page,int count)  {
        logger.info("/fineArt/myAddFine 我添加的 艺术精品, userId={}",  userId);
        ResponseObject ob = new ResponseObject();
        Map<String,Object>  fineArts;
        try {
            fineArts = fineArtService.myAddFine(userId,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(fineArts);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     我的草稿箱
     */
    @RequestMapping(value = "/myDraft", method = RequestMethod.POST)
    public ResponseObject myDraft(String userId,int page,int count)  {
        logger.info(" fineArt/myDraft, userId={}",  userId);
        ResponseObject ob = new ResponseObject();
        Map<String,Object> caogaos;
        try {
            caogaos= fineArtService.myDraft(userId,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(caogaos);
        ob.setMsg("success");
        return ob;
    }

    /**
     *     草稿箱的  根据id  查看具体内容进行编辑
     */
    @RequestMapping(value = "/drafts/edit", method = RequestMethod.POST)
    public ResponseObject biaji(String fineId)  {
        logger.info("/fineArt/drafts/edit 草稿箱编辑, fineId={}",  fineId);
        ResponseObject ob = new ResponseObject();
        Map<String,Object> fineArt;
        try {
            fineArt = fineArtService.bianji(fineId);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(fineArt);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     删除文章
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public ResponseObject delete(String fineId,String state)  {
        logger.info("/fineArt/delete 删除文章, fineId={},state={}",  fineId,state);
        ResponseObject ob = new ResponseObject();
        Integer designArticles;
        try {
            designArticles = fineArtService.delete(fineId,state );
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(designArticles);
        ob.setMsg("success");
        return ob;
    }


    /**
     *     由草稿箱 编辑文章 从新发布
     *
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseObject update(String fineId,String fineTitle, String fineContent, String draft, String fineImage, String list,String userId,String shopId)  {
        logger.info("/fineArt/update 编辑文章 从新发布,fineTitle={},fineContent={},draft={},fineImage={},list={},userId={}",fineTitle,fineContent,draft,fineImage,list,userId);
        ResponseObject ob = new ResponseObject();
        Integer designArticles;
//        try {
            designArticles = fineArtService.update(fineId,fineTitle,fineContent,draft,fineImage,list,userId,shopId);
//        } catch (Exception e) {
//            ob.setCode(1);
//            ob.setMsg(e.getMessage());
//            return ob;
//        }
        ob.setCode(0);
        ob.setData(designArticles);
        ob.setMsg("success");
        return ob;
    }
    //个人主页  精品  zijikanziji
    @RequestMapping(value = "/personalSoft", method = RequestMethod.POST)
    @BussinessLog(value = "/fineArt/personalSoft 个人主页  精品 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject personalSoft(String userId,int page,int count) {
        ResponseObject ob = new ResponseObject();
        ob.setCode(0);
        ob.setMsg("success");
        ob.setData(fineArtService.personalSoft(userId,page,count));
        return ob;
    }



    //个人主页  艺术精品  别人看你
    @RequestMapping(value = "/personalSoftOrder", method = RequestMethod.POST)
    public ResponseObject personalSoftOrder(String userId,int page,int count,String coverUserId) {
        logger.info("/fineArt/personalSoftOrder 个人主页  艺术精品,userId={},page={},count={},coverUserId={}",userId,page,count,coverUserId);
        ResponseObject ob = new ResponseObject();
        ob.setCode(0);
        ob.setMsg("success");
        ob.setData(fineArtService.personalSoftOrder(userId,page,count,coverUserId));
        return ob;
    }


    //搜索
    @RequestMapping(value = "/searchFine", method = RequestMethod.POST)
    @BussinessLog(value = "/fineArt/searchFine 搜索 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject searchDesign(String value,String userId, int page, int count) {
        ResponseObject ob = new ResponseObject();
        Map<String,Object> integer;
        try {
            integer = fineArtService.searchDesign(value,userId,page,count);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }

    //用户选择次数
    @RequestMapping(value = "/userCount", method = RequestMethod.POST)
    @BussinessLog(value = "/fineArt/userCount 用户选择次数 ", platform = PlatformEnum.WEB, save = false)
    public ResponseObject searchDesign(String id,String state) {
        ResponseObject ob = new ResponseObject();
        int integer;
        try {
            integer = fineArtService.userCount(id,state);
        } catch (Exception e) {
            ob.setCode(1);
            ob.setMsg(e.getMessage());
            return ob;
        }
        ob.setCode(0);
        ob.setData(integer);
        ob.setMsg("success");
        return ob;
    }
}
